//
//  WmcMediaAlbumAPI.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/6/10.
//

import Foundation
import RxSwift
import YPNetwork

struct WmcMediaAlbumAPI {
    /// 查询相册
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=302804&mod=511883&itf=2269190
    /// - Parameters:
    ///   - albumType: 相册类型，0：个人
    ///   - businessId: 业务id，albumType为0时是鱼泡用户id
    ///   - page: 当前页号
    ///   - pageSize: 分页大小
    ///   - wmIds: 水印类型id
    ///   - date: yyyy-MM-dd格式
    ///   - resourceType: 资源类型 0：图片  1：视频   不传：所有
    /// - Returns: Observable<WmcMediaPhotoModel>
    static func fetchPhotoList(page: Int, pageSize: Int, wmIds: [Int]? = nil, startDate: String? = nil, endDate: String? = nil, resourceType: [Int]? = nil) -> Observable<[WmcMediaPhotoModel]> {
        var params: [String: Any] = ["albumType": 0, "currentPage": page, "pageSize": pageSize]

        if let userId = WmcLoginManager.shared.yupaoAccountModel?.userId {
            params["busId"] = userId
        }
        
        if let wmIds = wmIds {
            params["wmIds"] = wmIds
        }

        if let startDate = startDate {
            params["startDate"] = startDate
        }
        
        if let endDate = endDate {
            params["endDate"] = endDate
        }

        if let resourceType = resourceType {
            params["resourceType"] = resourceType
        }

        return WmcMediaRequest.requestData(path: "/media/v1/album/list", parameters: params) { request in
            request.method = .post
        }.parseListModelWithClass(WmcMediaPhotoModel.self, designatedPath: "data")
    }
    
    static func fetchPhotoList(page: Int, pageSize: Int, wmIds: [Int]? = nil, startDate: String? = nil, endDate: String? = nil, resourceType: [Int]? = nil, success: @escaping ((list: [WmcMediaPhotoModel], total: Int)) -> Void, failure: @escaping YPNetworkRequestFailure) {
        var params: [String: Any] = ["albumType": 0, "currentPage": page, "pageSize": pageSize]

        if let userId = WmcLoginManager.shared.yupaoAccountModel?.userId {
            params["busId"] = userId
        }
        
        if let wmIds = wmIds {
            params["wmIds"] = wmIds
        }

        if let startDate = startDate {
            params["startDate"] = startDate
        }
        
        if let endDate = endDate {
            params["endDate"] = endDate
        }

        if let resourceType = resourceType {
            params["resourceType"] = resourceType
        }

        WmcMediaRequest.requestData(path: "/media/v1/album/list", parameters: params) { request in
            request.method = .post
        } success: { (response: YPNetworkResponse) in
            if let dic = response.data as? [String: Any],
               let list = dic["data"] as? [Any],
               let paginator = dic["paginator"] as? [String: Any]
            {
                let listArray = list.parseListModelWithClass(WmcMediaPhotoModel.self)
                let total = (paginator["totalRecord"] as? Int) ?? 0
                let result = (list: listArray, total: total)
                success(result)
            } else {
                failure(.init(code: 0, message: "参数解析失败！"))
            }
        } failure: { error in
            failure(error)
        }
    }

    /// 删除照片
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=302804&mod=511883&itf=2269186
    /// - Parameters:
    ///   - albumType: 相册类型，0：个人
    ///   - businessId: 资源id，albumType为0时是鱼泡用户id
    ///   - id: 相册资源id
    /// - Returns:  Observable<YPNetworkResponse>
    static func deletePhoto(id: Int) -> Observable<YPNetworkResponse> {
        var params: [String: Any] = ["albumType": 0, "id": id]
        
        if let userId = WmcLoginManager.shared.yupaoAccountModel?.userId {
            params["busId"] = userId
            params["appId"] = 101
        }

        return WmcMediaRequest.requestData(path: "/media/v1/album/delete", parameters: params) { request in
            request.method = .post
        }
    }

    /// 日历获取拍照的日期
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=302804&mod=511883&itf=2270078
    /// - Parameters:
    ///   - albumType: 相册类型，0：个人
    ///   - businessId: 资源id，albumType为0时是鱼泡用户id
    ///   - startDate: 开始时间 yyyy-MM-dd格式
    ///   - endDate: 结束时间 yyyy-MM-dd格式
    ///   - resourceType: 资源类型 0：图片  1：视频   不传：所有
    /// - Returns: Observable<[WmcMediaTakePhotoDayModel]>
    static func fetchTakePhotoCalendar(startDate: String, endDate: String, resourceType: [Int]? = nil) -> Observable<[WmcMediaTakePhotoDayModel]> {
        var params: [String: Any] = ["albumType": 0, "startDate": startDate, "endDate": endDate]

        if let userId = WmcLoginManager.shared.yupaoAccountModel?.userId {
            params["busId"] = userId
        }
        
        if let resourceType = resourceType {
            params["resourceType"] = resourceType
        }

        return WmcMediaRequest.requestData(path: "/media/v1/album/getTakeDate", parameters: params) { request in
            request.method = .post
        }.parseListModelWithClass(WmcMediaTakePhotoDayModel.self, designatedPath: "takeDateList")
    }

    /// 分类照片查询
    /// DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=302804&mod=511883&itf=2270108
    /// - Parameters:
    ///   - albumType: 相册类型，0：个人
    ///   - businessId: 资源id，albumType为0时是鱼泡用户id
    ///   - resourceType: 资源类型 0：图片  1：视频   不传：所有
    /// - Returns: Observable<[WmcMediaAlbumModel]>
    static func fetchAlbumList(resourceType: [Int]? = nil) -> Observable<[WmcMediaAlbumModel]> {
        var params: [String: Any] = ["albumType": 0]

        if let userId = WmcLoginManager.shared.yupaoAccountModel?.userId {
            params["busId"] = userId
        }

        if let resourceType = resourceType {
            params["resourceType"] = resourceType
        }

        return WmcMediaRequest.requestData(path: "/media/v1/album/classifyList", parameters: params) { request in
            request.method = .post
        }.parseListModelWithClass(WmcMediaAlbumModel.self, designatedPath: "allResources")
    }

    /// 水印分类获取查询
    /// - DocumentUrl: http://rap2.taobao.org/organization/repository/editor?id=302804&mod=511883&itf=2270164
    /// - Parameters:
    ///   - albumType: 相册类型，0：个人
    ///   - businessId: 资源id，albumType为0时是鱼泡用户id
    ///   - resourceType: 资源类型 0：图片  1：视频   不传：所有
    /// - Returns: Observable<[WmcMediaPhotographStatisticsModel]>
    static func fetchTakePhotoStatistics(resourceType: [Int]? = nil) -> Observable<[WmcMediaPhotographStatisticsModel]> {
        var params: [String: Any] = ["albumType": 0]
        
        if let userId = WmcLoginManager.shared.yupaoAccountModel?.userId {
            params["busId"] = userId
        }

        if let resourceType = resourceType {
            params["resourceType"] = resourceType
        }

        return WmcMediaRequest.requestData(path: "/media/v1/album/watermarkList", parameters: params) { request in
            request.method = .post
        }.parseListModelWithClass(WmcMediaPhotographStatisticsModel.self, designatedPath: "albumWaterMarkList")
    }

    /// 获取指定时间以前最近一次拍照时间
    /// - Parameters:
    ///   - albumType: 相册类型，0：个人
    ///   - businessId: 资源id，albumType为0时是鱼泡用户id
    ///   - maxDate: 最大时间
    ///   - resourceType: 资源类型 0：图片  1：视频   不传：所有
    /// - Returns: Observable<YPNetworkResponse>
    static func fetchTakePhotoLastDate(albumType: Int, businessId: String, maxDate: String, resourceType: [Int]? = nil) -> Observable<YPNetworkResponse> {
        var params: [String: Any] = ["albumType": albumType, "busId": businessId, "maxDate": maxDate]

        if let resourceType = resourceType {
            params["resourceType"] = resourceType
        }

        return WmcMediaRequest.requestData(path: "/media/v1/album/getPreDate", parameters: params) { request in
            request.method = .post
        }
    }
    
    static func fetchAlbumShareInfo(wmIds: [Int]? = nil) -> Observable<WmcMediaShareModel> {
        var params: [String: Any] = ["albumType": 0]

        if let userId = WmcLoginManager.shared.yupaoAccountModel?.userId {
            params["busId"] = userId
        }

        if let wmIds = wmIds {
            params["wmIds"] = wmIds
        }

        return WmcMediaRequest.requestData(path: "/media/v1/album/shareAlbum", parameters: params) { request in
            request.method = .post
        }.parseModelWithClass(WmcMediaShareModel.self)
    }
    
    static func fetchVideoShareInfo(id: Int) -> Observable<WmcMediaShareModel> {
        var params: [String: Any] = ["albumType": 0, "albumResourceId": id]

        if let userId = WmcLoginManager.shared.yupaoAccountModel?.userId {
            params["busId"] = userId
            params["shareUid"] = userId
        }

        return WmcMediaRequest.requestData(path: "/media/v1/album/shareVideo", parameters: params) { request in
            request.method = .post
        }.parseModelWithClass(WmcMediaShareModel.self)
    }
}
